package com.admin.controller;

import com.bean.ResponseBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

@RestController
public class UploadAction {

    @Value("${upload.tmp.dir}")
    private String tmp;

    @RequestMapping("/upload")
    public ResponseBean upload(MultipartFile  file) throws IOException {
        //得到原来的文件名  xxxx.jpg
        String oldFileName = file.getOriginalFilename();
        //得到一个最后一个.的位置
        int index = oldFileName.lastIndexOf('.');
        //得到后辍名
        String extName = oldFileName.substring(index);
        //新的文件名
        String newFileName = System.currentTimeMillis()+extName;
        //传到tmp目录
        File descFile = new File(tmp, newFileName);
        file.transferTo(descFile);
        return new ResponseBean(0,0,newFileName);
    }
}
